//
//  GWMainViewController.m
//  新浪微博
// /Users/用户名/Library/Application Support/Developer/Shared/Xcode/Plug-ins
//  Created by qianfeng on 16/1/9.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWMainViewController.h"
#import "GWHomeViewController.h"
#import "GWMessageViewController.h"
#import "GWDiscoverViewController.h"
#import "GWMineViewController.h"
#import "GWMyNavigationController.h"
#import "GWMyTabBar.h"
#import "GWComposeViewController.h"
#import "GWUserInfoTool.h"
#import "GWAccount.h"
#import "GWAccountTool.h"

@interface GWMainViewController ()<GWMyTabBarDelegate>
@property (nonatomic,weak)GWHomeViewController *home;
@property (nonatomic,weak)GWMessageViewController *message;
@property (nonatomic,weak)GWMineViewController *mine;
@end

@implementation GWMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    GWHomeViewController *home =[[GWHomeViewController alloc]init];
    [self addChildWithViewController:home title:@"首页" imageName:@"tabbar_home" selectImageName:@"tabbar_home_selected"];
    self.home =home;
    
    GWMessageViewController *message =[[GWMessageViewController alloc]init];
    [self addChildWithViewController:message title:@"消息" imageName:@"tabbar_message_center" selectImageName:@"tabbar_message_center_selected"];
    self.message =message;
    
    GWDiscoverViewController *discover =[[GWDiscoverViewController alloc]init];
    [self addChildWithViewController:discover title:@"发现" imageName:@"tabbar_discover" selectImageName:@"tabbar_discover_selected"];
    
    GWMineViewController *mine =[[GWMineViewController alloc]init];
    [self addChildWithViewController:mine title:@"我" imageName:@"tabbar_profile" selectImageName:@"tabbar_profile_selected"];
    self.mine =mine;
    
    GWMyTabBar *myTabBar =[[GWMyTabBar alloc]init];
    myTabBar.delegate =self;
    [self setValue:myTabBar forKey:@"tabBar"];
    UITabBarItem *appearance =[UITabBarItem appearance];
    [appearance setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
    
    [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(unreadCount) userInfo:nil repeats:YES];
    // Do any additional setup after loading the view.
}
-(void)unreadCount
{
    GWUnReadCountParam *param = [GWUnReadCountParam param];
    param.uid =[GWAccountTool Account].uid;
    [GWUserInfoTool unreadCountWithParame:param success:^(GWUnReadCountResult *result) {
        if (result.status==0) {
            self.home.tabBarItem.badgeValue =nil;
        }
        else
        {
            self.home.tabBarItem.badgeValue =[NSString stringWithFormat:@"%d",result.status];
        }
        if (result.messageCount==0) {
            self.message.tabBarItem.badgeValue =nil;
        }
        else
        {
            self.message.tabBarItem.badgeValue =[NSString stringWithFormat:@"%d",result.messageCount];
        }
        if (result.follower==0) {
            self.mine.tabBarItem.badgeValue =nil;
        }
        else
        {
             self.mine.tabBarItem.badgeValue =[NSString stringWithFormat:@"%d",result.follower];
        }
        
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
            UIUserNotificationType myTypes = UIUserNotificationTypeBadge;
            UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil];
            [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        }
        
        [UIApplication sharedApplication].applicationIconBadgeNumber =result.totalCount;
       
    } failure:^(NSError *error) {
        
    }];
}
-(void)myTabBarDidPuls:(GWMyTabBar *)tabBar
{
    GWComposeViewController *cv =[[GWComposeViewController alloc]init];
    GWMyNavigationController *nav =[[GWMyNavigationController alloc]initWithRootViewController:cv];
    [self presentViewController:nav animated:YES completion:nil];
}
-(void)addChildWithViewController:(UIViewController *)vc title:(NSString *)title imageName:(NSString *)imageName selectImageName:(NSString *)selectImageName
{
    
    vc.title =title;
    [vc.tabBarItem setImage:[UIImage imageWithName:imageName]];
    [vc.tabBarItem setSelectedImage:[[UIImage imageWithName:selectImageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    GWMyNavigationController *nav =[[GWMyNavigationController alloc]initWithRootViewController:vc];
    [self addChildViewController:nav];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
